//
//  VRReportView.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/14.
//

import UIKit

class VRLiveReportView: UIView, SAAnimationDelegate, SAAnimationProtocol {

    func animationContainerViewSize() -> CGSize {
        CGSize(width: .screenWidth, height: 470.0)
    }
    
    func animationContainerViewStyle() -> SAAnimationViewStyle {
        .center
    }
    
//    func animationContainerScreenInteraction() -> EKAttributes.UserInteraction {
//        .dismiss
//    }
    private lazy var titleLabel = c_make_ex(UILabel.self)
        .color(.white).font(18.font_bold).alignment(.center)
        .text("Report this live broadcast").end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.tableFooterView = UIView(frame: .zero)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = 40
        tableView.register(VRLiveReportItemCell.self)
        return tableView
    }()
    private lazy var cancelButton = c_make_ex(UIButton.self)
        .color("#FFFC00").font(18.font_bold).text("Cancel")
        .corner(25).border("#FFFC00", width: 2).end()
    private lazy var sumbitButton = c_make_ex(UIButton.self)
        .color(.black).font(18.font_bold).text("Submit")
        .corner(25).bgColor("#FFFC00").end()
    private var dataSource: [VRReportItemModel] =
    [VRReportItemModel(title: "Pornographic and vulgar", select: false),
     VRReportItemModel(title: "Underage", select: false),
     VRReportItemModel(title: "Gambling and drugs", select: false),
     VRReportItemModel(title: "Terrorism, Vioence, Bloodiness", select: false),
     VRReportItemModel(title: "Dangerous behavior", select: false),
     VRReportItemModel(title: "Tobaco and alcohol", select: false),
     VRReportItemModel(title: "Politics and religion", select: false)]
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = "#1F1E18".toColor
        self.fj.corner(20, corners: [.layerMinXMinYCorner,.layerMaxXMinYCorner])
        
        subviews{
            titleLabel
            tableView
            cancelButton
            sumbitButton
        }
        
        titleLabel.top(20).centerHorizontally()
        tableView.top(57).leading(0).trailing(0).height(290)
        cancelButton.leading(25).height(50)
        sumbitButton.trailing(25).height(50)
        cancelButton.Top == tableView.Bottom + 15
        sumbitButton.CenterY == cancelButton.CenterY
        sumbitButton.Leading == cancelButton.Trailing + 15
        cancelButton.Width == sumbitButton.Width
        
        cancelButton.addTarget(self, action: #selector(clickCancelButton), for: .touchUpInside)
        sumbitButton.addTarget(self, action: #selector(clickSubmitButton), for: .touchUpInside)
    }
    
    @objc private func clickCancelButton(){
        saAnimate.animation(dismiss: self)
    }
    
    @objc private func clickSubmitButton(){
        saAnimate.animation(dismiss: self){
            
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension VRLiveReportView: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(VRLiveReportItemCell.self, indexPath: indexPath)
        cell.itemModel = dataSource[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        dataSource[indexPath.row].select = !dataSource[indexPath.row].select
        tableView.reloadData()
        
    }
}

class VRLiveReportItemCell: UITableViewCell {

    private let titleLabel = c_make_ex(UILabel.self)
        .color(.white.alpha(0.9)).font(16.font_medium).end()
    private let selButton = c_make_ex(UIButton.self)
        .image("Report_unselect").image("Report_select",state: .selected).end()
        
    var itemModel: VRReportItemModel? {
        didSet{
            guard let md = itemModel else {
                return
            }
            titleLabel.text = md.title
            selButton.isSelected = md.select
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            titleLabel
            selButton
        }
        
        titleLabel.leading(15).centerVertically()
        selButton.trailing(5).size(40).centerVertically()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
